/*
 * Copyright 2012 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.editor.controller.component;

import java.awt.Point;
import java.awt.event.ActionEvent;
import java.net.URL;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import offset.nodes.client.chooser.view.NodeChooserDialog;
import offset.nodes.client.editor.Editor;
import offset.nodes.client.editor.controller.component.ComponentKit.ComponentUI;
import offset.nodes.client.editor.controller.EditorActions;
import offset.nodes.client.editor.controller.EditorKit;
import offset.nodes.client.editor.model.ComponentModel;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.view.component.ComponentDialog;
import offset.nodes.client.view.component.ComponentDialogDialog;

/**
 * The collection of actions associated to components.
 * 
 * @author
 * Walter
 * Lütgenau
 */
public class ComponentActions {

    /**
     * An abstract class for all component actions.
     */
    public static abstract class AbstractComponentAction extends EditorActions.AbstractEditorAction {

        public AbstractComponentAction(String name, Editor editor) {
            super(name, editor);
        }

        protected ComponentModel getModel(ActionEvent ae) {
            return new ComponentModel(getDocumentContext(ae), getEditor());
        }
    }

    /**
     * Select a node to be added as a component page to the editor.
     */
    public static class SelectComponentAction extends AbstractComponentAction {

        public void action(ActionEvent e) throws Exception {
            ServerModel server = new ServerModel(new URL((String) getEditor().getProperties().get(Editor.PROP_UPLOAD_TO)));
            String root = (String) getEditor().getProperties().get(Editor.PROP_DOCUMENT_PATH);
            
            NodeChooserDialog dialog = new NodeChooserDialog(new javax.swing.JFrame(), true, server, root, "*");
            dialog.setLocationByPlatform(true);
            dialog.setTitle(ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("component.select.dialog"));

            dialog.setVisible(true);

            if (dialog.getReturnStatus() == NodeChooserDialog.RET_OK) {
                if (!((EditorKit) getEditor().getBrowserPane().getEditorKit()).getComponentKit().isEditablePage(dialog.getUuid())) {
                    JOptionPane.showMessageDialog(getEditor(), java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("component.notEditable"));
                    return;
                }

                int pos = getEditor(e).getCaretPosition();

                getModel(e).insertComponent(pos, dialog.getUuid(), dialog.getPath());
            }
        }

        public SelectComponentAction(Editor editor) {
            super(java.util.ResourceBundle.getBundle("offset/nodes/client/editor/view/resources/EditorBundle").getString("component.select"), editor);
        }
    }
    
   /**
     * Edit the component in its own frame.
     */
    public static class EditAsDialog extends AbstractComponentAction {
        ComponentUI componentUI;

        public void action(ActionEvent e) throws Exception {
            componentUI.prepareForDialog();
            ComponentDialogDialog dialog = new ComponentDialogDialog((ComponentDialog) componentUI.getComponent(), true);
            dialog.setSize(getEditor().getSize());
            Point editorLocation = getEditor().getLocationOnScreen();
            dialog.setLocation(editorLocation.x + 100, editorLocation.y + 100);
            dialog.setVisible(true);
            
            componentUI.prepareForEmbedded();
        }

        public EditAsDialog(ComponentUI componentUI, Editor editor) {
            super("", editor);
            setIcon(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/sc_component_dialog.png")));   
            this.componentUI = componentUI;
        }
    }
}
